import { DataTypes, Model, Optional } from 'sequelize';
import sequelize from '../utils/database';
import Category from './Category';
import Brand from './Brand';

// 定义商品属性接口
interface ProductAttributes {
  id: number;
  name: string;
  subtitle?: string;
  categoryId: number;
  brandId?: number;
  spuCode?: string;
  mainImage?: string;
  images?: string[];
  videoUrl?: string;
  price: number;
  marketPrice?: number;
  costPrice?: number;
  stockQuantity: number;
  saleCount: number;
  unit: string;
  weight?: number;
  volumeLength?: number;
  volumeWidth?: number;
  volumeHeight?: number;
  keywords?: string;
  description?: string;
  specifications?: object;
  serviceInfo?: string;
  shippingInfo?: string;
  status: number;
  isRecommend: number;
  isNew: number;
  isHot: number;
  sortOrder: number;
  createdAt?: Date;
  updatedAt?: Date;
}

// 定义创建商品时的可选属性
interface ProductCreationAttributes extends Optional<ProductAttributes, 'id' | 'saleCount' | 'createdAt' | 'updatedAt'> {}

// 定义商品模型类
class Product extends Model<ProductAttributes, ProductCreationAttributes> implements ProductAttributes {
  public id!: number;
  public name!: string;
  public subtitle?: string;
  public categoryId!: number;
  public brandId?: number;
  public spuCode?: string;
  public mainImage?: string;
  public images?: string[];
  public videoUrl?: string;
  public price!: number;
  public marketPrice?: number;
  public costPrice?: number;
  public stockQuantity!: number;
  public saleCount!: number;
  public unit!: string;
  public weight?: number;
  public volumeLength?: number;
  public volumeWidth?: number;
  public volumeHeight?: number;
  public keywords?: string;
  public description?: string;
  public specifications?: object;
  public serviceInfo?: string;
  public shippingInfo?: string;
  public status!: number;
  public isRecommend!: number;
  public isNew!: number;
  public isHot!: number;
  public sortOrder!: number;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  // 关联属性
  public readonly category?: Category;
  public readonly brand?: Brand;
}

// 初始化商品模型
Product.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    name: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: '商品名称',
    },
    subtitle: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '副标题/卖点',
    },
    categoryId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '分类ID',
      field: 'category_id',
    },
    brandId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '品牌ID',
      field: 'brand_id',
    },
    spuCode: {
      type: DataTypes.STRING(50),
      allowNull: true,
      unique: true,
      comment: 'SPU编码',
      field: 'spu_code',
    },
    mainImage: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: '主图',
      field: 'main_image',
    },
    images: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '商品图片集',
    },
    videoUrl: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: '商品视频',
      field: 'video_url',
    },
    price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      comment: '销售价格',
    },
    marketPrice: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: '市场价格',
      field: 'market_price',
    },
    costPrice: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: '成本价格',
      field: 'cost_price',
    },
    stockQuantity: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '库存数量',
      field: 'stock_quantity',
    },
    saleCount: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '销量',
      field: 'sale_count',
    },
    unit: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: '件',
      comment: '单位',
    },
    weight: {
      type: DataTypes.DECIMAL(8, 3),
      allowNull: true,
      comment: '重量(kg)',
    },
    volumeLength: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '长(mm)',
      field: 'volume_length',
    },
    volumeWidth: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '宽(mm)',
      field: 'volume_width',
    },
    volumeHeight: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '高(mm)',
      field: 'volume_height',
    },
    keywords: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '关键词',
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '商品详情',
    },
    specifications: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '商品规格参数',
    },
    serviceInfo: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '服务信息',
      field: 'service_info',
    },
    shippingInfo: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '配送信息',
      field: 'shipping_info',
    },
    status: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 1,
      comment: '状态：0-下架，1-上架，2-预售',
    },
    isRecommend: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 0,
      comment: '是否推荐：0-否，1-是',
      field: 'is_recommend',
    },
    isNew: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 0,
      comment: '是否新品：0-否，1-是',
      field: 'is_new',
    },
    isHot: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 0,
      comment: '是否热卖：0-否，1-是',
      field: 'is_hot',
    },
    sortOrder: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '排序',
      field: 'sort_order',
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '创建时间',
      field: 'created_at',
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '更新时间',
      field: 'updated_at',
    },
  },
  {
    sequelize,
    tableName: 'products',
    timestamps: true,
    underscored: true,
    indexes: [
      {
        fields: ['category_id'],
      },
      {
        fields: ['brand_id'],
      },
      {
        fields: ['status'],
      },
      {
        fields: ['is_recommend'],
      },
      {
        fields: ['is_new'],
      },
      {
        fields: ['is_hot'],
      },
      {
        type: 'FULLTEXT',
        fields: ['name', 'keywords'],
      },
    ],
  }
);

// 定义关联关系
Product.belongsTo(Category, {
  foreignKey: 'categoryId',
  as: 'category',
});

Product.belongsTo(Brand, {
  foreignKey: 'brandId',
  as: 'brand',
});

export default Product;